<?php
	function SmfAfterComponentInitHook($ctrl)
	{
		if(cr('cmsInstalled') && @strpos($_SERVER['REQUEST_URI'],'/cms/startTestSession') === false)
		{
			Configure::registerConfigModelLoder("SmfConfig",'Smf.SmfConfig');
		}
	}
	
	function SmfAfterLoginHook(CmsUsersController $ctrl)
	{
		$userId = $ctrl->Session->read("Auth.User.id");
		if(cr("SmfConfig.forum_path") && checkSessionKey('smf'))
			cri("Smf.SmfApi")->login($userId);	
	}
	
	function SmfAfterRegisterHook(CmsUsersController $ctrl,$user)
	{
		$userId = @$user['id'];
		if($userId && checkSessionKey('smf'))
		{
			if(cr("SmfConfig.forum_path") && cri("Smf.SmfApi")->register($userId,$user))
			{
				if($ctrl->Session->read("Auth.User"))
					cri("Smf.SmfApi")->login($userId);
			}
			
		}
	}
	
	function SmfOnLogoutHook($ctrl)
	{
		if(cr("SmfConfig.forum_path"))
		{
			cri("Smf.SmfApi")->logout($ctrl->Session->read("Auth.User.id"));
		}	
	}
	
	function SmfAfterEditProfileHook($ctrl)
	{
		
		if(cr("SmfConfig.forum_path") && checkSessionKey('smf'))
		{
			$UserModel = cri("Users.User");
			$UserModel->recursive = -1;
			$user = $UserModel->findById($ctrl->Session->read("Auth.User.id"));
			if($user)
			{
				cri("Smf.SmfApi")->updateSmfUser($user['User']['id'],$user['User']);
			}
		}
	}
	
	function SmfAfterUserDeleteHook($ctrl,$userId)
	{
		if($userId && cr("SmfConfig.forum_path") && checkSessionKey('smf'))
		{
			cri("Smf.SmfApi")->deleteSmfUser($userId);
		}
	}
?>